// pages/wangjimima/wangjimima.js
var app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fasongurl: app.data.url +'/zhongxue/maoapi/fasong',
    changepwd: app.data.url + '/zhongxue/maoapi/changepwd',
    phones: '',
    code:'',
    pwd: '',
    fasongyanzhen:"发送验证码",
    kong:true,
    kongs:true,
    src: app.data.url +"/uploads/zhongxue/damao.png",
    yanzhengma:app.data.url +"/uploads/zhongxue/yanzhnegmas.png",
    username: app.data.url +"/uploads/zhongxue/shouji.png",
    password: app.data.url +"/uploads/zhongxue/passwords.png",
    loginbag: app.data.url +"/uploads/zhongxue/loginbag.png",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


  },
  bindKeyInput:function(e){
    // console.log(e);
    var value = e.detail.value;
    // console.log(value);
    this.setData({ phones:value});
  },
  bindKeyInputs:function(e){
    var value = e.detail.value;
    this.setData({code:value});
  },
  bindKeyInputss:function(e){
    var value = e.detail.value;
    this.setData({ pwd: value });
  },
  fasong:function(){
    var kong = this.data.kong;
    if(!kong){
      return
    }
    var phones = this.data.phones;
    if(!phones){
      wx.showToast({
        title: '请填入手机号码',
        duration: app.data.quanbushijian
      })
      return
    }
    this.setData({ kong: false });
    var user = wx.getStorageSync('user');
    var that = this;
    wx.showLoading({
      title: '发送中',
    })
    wx.request({
      url: that.data.fasongurl,
      data: { token: user.token, phones: phones},
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        var data = res.data;
        console.log(data);
        if(data.code==200){
          wx.showToast({
            title: '发送成功',
            duration: app.data.quanbushijian
          })
          ea.TimeDown(60,function(time){
            that.setData({fasongyanzhen:time});
            if(time==0){
              that.setData({ kong: true, fasongyanzhen:"发送验证码"});
            }
          });
        }else{
          that.setData({kong:true})
          wx.showToast({
            title: data.msg,
            icon:'none',
            duration: app.data.quanbushijian
          })
        }
      },
      fail: function(res) {
        that.setData({ kong:true});
      },
      complete: function(res) {
        wx.hideLoading()
      },
    })
  },
  login:function(){
    var kongs = this.data.kongs;
    if(!kongs){
      return
    }
    var tiaojian = {};
    var user = wx.getStorageSync('user');
    tiaojian.token = user.token;
    var phones = this.data.phones;
    var code = this.data.code;
    var pwd = this.data.pwd;
    if (!phones){
      wx.showToast({
        title: '请输入手机号',
        icon:'none',
        duration: app.data.quanbushijian
      })
      return
    }
    if (!code) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: app.data.quanbushijian
      })
      return
    }
    if (code.length<4) {
      wx.showToast({
        title: '请输入4位验证码',
        icon: 'none',
        duration: app.data.quanbushijian
      })
      return
    }
    if (!pwd) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: app.data.quanbushijian
      })
      return
    }
    tiaojian.phones = phones;
    tiaojian.code = code;
    tiaojian.pwd = pwd;
    this.setData({kongs:false});
    var that = this;
    wx.showLoading({
      title: '修改中',
    })
    wx.request({
      url: that.data.changepwd,
      data: tiaojian,
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        var data = res.data;
        if(data.code==200){
          wx.showToast({
            title: '修改成功',
            duration: app.data.quanbushijian
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1500)
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none',
            duration: app.data.quanbushijian
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {
        wx.hideLoading()
        that.setData({kongs:true})
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/indexfirst/indexfirst',
    }
  }
})